<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 	
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
<link rel="stylesheet" href="/css/jquery.tooltip.css" />
<script src="/js/jquery.tooltip.min.js" type="text/javascript"></script>
<script src="/js/yaodian100.common.js" type="text/javascript"></script>
	<script language="javascript"><!--
	var combineMajor = "";
	var temp ="";
	$().ready( function() {
		//删除主规格
		$("a[id*='mainDel_']").click(function(){
			deleteRow($(this));
		});
		//删除次规格
		$("a[id*='secondDel_']").click(function(){
			deleteRow($(this));
		});

		/*
		$("#saveMutilMajor").click(function(){
			$("input[name='majorId']").each(function(){
				combineMajor=$(this).val();
				$("input[id*='specCode_" + $(this).val() + "']").each(function(i,item){
					combineMajor = combineMajor + ","+item.value;
				});
				temp = temp + combineMajor +",";
			});
			alert(temp);
		});

		*/
		$("tr[id*='tdDesc_']").tooltip();
			$('#arrivalDate').datepick({minDate: '+1D', showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-1:+1'});
	    	
			$("input[name='spcReverseInventory']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcMaxCountPerOrder']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcMinCountPerOrder']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcSafeInventory']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='showSpecSeq']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcHotSaleInventory']").each(function(i,item){
				$(this).numeric();
			});
			
			$("input[id*='spcName_']").each(function(i,item){					
				if(item.value != ""){
					$(this).attr("readonly",true);
				}
			});

			$("#submitStock").click(function(){
				if ( "" == $("#arrivalDate").val() ){
					alert("请输入预计到货日");
					return false;
				}
				$("#specForm").attr("action","updateArrivalDate.do");
				$("#specForm").submit();
			});
			/*
			$("input[id*='spcHotSaleInventory_']").blur(function(){
				sumInv();
			});
			*/
			$("#submit_button").click(function(){
				try {
					$("input[id*='spcName_']").each(function(i,item){					
						if(item.value == ""){
							alert("请输入规格名称");
							item.focus();
							throw item;
						}
					});
					$("input[id*='spcReverseInventory_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入库存保留量");
							item.focus();
							throw item;
						}
						
					});
								
					$("input[id*='spcMaxCountPerOrder_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入订单,单笔最大可销售数");
							item.focus();
							throw item;
						}
						if(parseInt(item.value)<parseInt($("input[id='spcMinCountPerOrder_"+(i+1)+"']").val())){
							alert("单笔最大可销售数不可小於 单笔最小可销售数");
							item.focus();
							throw item;
						}
					});
					$("input[id*='spcMinCountPerOrder_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入订单,单笔最小可销售数");
							item.focus();
							throw item;
						}
						if(parseInt(item.value)<=parseInt(0)){
							alert("单笔最小可销售数要大於0");
							item.focus();
							throw item;
						}
					});
				
					$("input[id*='spcSafeInventory_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入安全库存量");
							item.focus();
							throw item;
						}
					});		
					$("input[id*='spcHotSaleInventory_']").each(function(i,item){
						if ( item.value == "" ){
							alert("热销 数量要大於等于0");
							item.focus();
							throw item;
						}
					});
					$("input[id*='showSeq_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入排序【0～99】");
							item.focus();
							throw item;
						}
					});
				}catch(e){
					return false;
				}

				if ($("#spcName").val() == "" && ($("#spcReverseInventory").val() != ""  || $("#spcMaxCountPerOrder").val() != "" ||  $("#spcMinCountPerOrder").val() != "")){
					alert("请输入规格名称");
					$("#spcName").focus();
					return false;
					
				}
				if ($("#spcName").val() != "" ){
					if ($("#spcReverseInventory").val() == "" ){
						alert("请输入库存保留量");
						$("#spcReverseInventory").focus();
						return false;
					}
					if ($("#spcMaxCountPerOrder").val() == "" ){
						alert("请输入订单,单笔最大可销售数");
						$("#spcMaxCountPerOrder").focus();
						return false;
					}
					if ($("#spcMinCountPerOrder").val() == "" ){
						alert("请输入订单,单笔最小可销售数");
						$("#spcMinCountPerOrder").focus();
						return false;
					}
					if(parseInt($("#spcMaxCountPerOrder").val())<parseInt($("#spcMinCountPerOrder").val())){
						alert("单笔最大可销售数不可小於 单笔最小可销售数");
						$("#spcMaxCountPerOrder").focus();
						return false;
					}
					if(parseInt($("#spcMinCountPerOrder").val())<=parseInt(0)){
						alert("单笔最小可销售数要大於0");
						item.focus();
						throw item;
					}
					if ($("#spcSafeInventory").val() == "" ){
						alert("请输入安全库存量");
						$("#spcSafeInventory").focus();
						return false;
					}
					if ($("#spcHotSaleInventory").val() == "" ){
						alert("热销 数量要大於等于0");
						$("#spcHotSaleInventory").focus();
						return false;
					}					
					if ($("#showSeq").val() == "" ){
						alert("请输入排序【0～99】");
						$("#showSeq").focus();
						return false;
					}
				}

				//若该商品已被列入搭售时,规格只能有一个,不可以再新增规格
				try{
					if(parseInt($("#specListsize").val())>0){
						var productId=$("#productId").val();
						$.getJSON("<%=ServerValue.getMidContextPath()%>/product/findAddonProdSpec.do?type=json", 
								{productId : productId}
							,function(jsonAddonProdSpec){
								if(jsonAddonProdSpec=='canOnlyOneSpec'){
									alert('该商品已被列入搭售时,规格只能有一个,不可以再新增规格');
									throw "canOnlyOneSpec";
								}
						});
					}
				}catch(e){
					return false;
				}
				$("#specForm").submit();
			});

	});
--></script>			
<h1>商品管理 - 商品规格<br/>
	<div class="f-link">
		<a href="<%=ServerValue.getMidContextPath()%>/product/list.do">回商品列表页</a>
	</div>
</h1>
<s:actionerror />
<s:actionmessage />
<s:fielderror />
<br/>
<!-- include product step menu -->
<jsp:include page="commonMenu2.jsp"/>
<table class="tb-data" width="100%">
	<tr>
		<th>商品名称<br/>（商品编号）</th>
		<td>${product.productName}(${productId})</td>
	</tr>
</table>
<br/><br/>
<s:form action="saveMajor.do" method="post" id="majorForm">	
	<table class="tb-data" width="100%">
		<thead>
  		<tr>
		   	<th width="80px">主规格</th>
		   	<th width="50px"><span class="txt-red">*</span>排序</th>
		   	<th width="50px"><span class="txt-red">*</span>删除</th>
		   	<td colspan="13">
			   	<table class="tb-data" width="100%">
				   	<tr>
					   	<th width="130px">次规格</th>
					   	<th width="140px">规格料号</th>
					   	<th width="140px">规格条码</th>
					   	<th width="100px">单笔订单<br/>最大可售数</th>
					    <th width="90px">单笔订单<br/>最小可售数</th>
					    <th width="90px">安全库存量</th>
							<th width="50px">排序</th>
							<th width="50px">删除</th>
				   	</tr>
			   	</table>
		   	</td>
	    </tr>
	  </thead>
		<tbody>
			<c-rt:if test="${empty addType}">${product.mainSpecName}
				<c-rt:forTokens items="${product.mainSpecName}" var="major" varStatus="mIndex" delims=",">
					<tr>
						<td width="80px">${major}
							<input type="hidden" name="majorName" id="majorName_${mIndex.count}" value="${major}" />
							<input type="hidden" name="majorId" value=""/>
						</td>
						<td width="50px"><input type="text" name="mainShowSeq" maxlength="2" id="mainShowSeq_${mIndex.count}" size="2" value="1" /></td>
						<td width="50px"><a href="###" id="mainDel_${mIndex.count}_${sIndex.count}">删除</a></td>
						<td colspan="13">
							<table class="tb-data" width="100%">
								<c-rt:forTokens items="${product.secondSpecName}" var="spec" varStatus="sIndex" delims=",">
									<tr>
										<td width="130px">${spec}
											<input type="hidden" name="spcName" id="spcName_${mIndex.count}_${sIndex.count}" value="${spec}" />
											<input type="hidden" name="spcId" value=""/>
											<input type="hidden" name="spcMajorName" value="${major}"/>${major}
										</td>
										<td width="140px"><input type="text" name="spcCode" id="spcCode_${mIndex.count}_${sIndex.count}" value="" size="20" maxlength="20"/></td>
										<td width="140px"><input type="text" name="spcBarCode" id="spcBarCode_${mIndex.count}_${sIndex.count}" value="" size="20" maxlength="20"/></td>
										<td width="90px"><input type="text" name="spcMaxCountPerOrder" id="maxCountPerOrder_${mIndex.count}_${sIndex.count}" value="" size="3" maxlength="3"/></td>
										<td width="90px"><input type="text" name="spcMinCountPerOrder" id="minCountPerOrder_${mIndex.count}_${sIndex.count}" value="" size="3" maxlength="3"/></td>
										<td width="100px"><input type="text" name="spcSafeInventory" id="SafeInventory_${mIndex.count}_${sIndex.count}" value="" size="3" maxlength="3"/></td>
										<td width="50px"><input type="text" name="showSpecSeq" id="showSpecSeq_${mIndex.count}_${sIndex.count}" value="1" size="3" maxlength="3"/></td>
										<td width="50px">
											<a href="###" id="secondDel_${mIndex.count}_${sIndex.count}">删除</a>
										</td>
									</tr>
								</c-rt:forTokens>
							</table>
						</td>
					</tr>
				</c-rt:forTokens>
			</c-rt:if>
			<c-rt:if test="${'major' eq addType}">
				<c-rt:forEach items="${mainSpecName}" var="major" varStatus="mIndex">
					<tr>
						<td width="80px">${major}
							<input type="hidden" name="majorName" maxlength="2" id="majorName_${mIndex.count}" size="2" value="${major}" />
							<input type="hidden" name="majorId" value=""/>
						</td>
						<td width="50px"><input type="text" name="mainShowSeq" maxlength="2" id="mainShowSeq_${mIndex.count}" size="2" value="1" /></td>
						<td width="50px"><a href="###" id="mainDel_${mIndex.count}_${sIndex.count}">删除</a></td>
						<td colspan="13">
							<table class="tb-data" width="100%">
								<c-rt:forEach items="${addSpecSel}" var="spec" varStatus="sIndex">
									<tr>
										<td width="130px">${spec}
											<input type="hidden" name="spcName" maxlength="2" id="spcName_${mIndex.count}_${sIndex.count}" size="2" value="${spec}" />
											<input type="hidden" name="spcMajorName" value="${major}"/>
											<input type="hidden" name="spcId" value=""/>
										</td>
										<td width="140px"><input type="text" name="spcCode" id="spcCode_${mIndex.count}_${sIndex.count}" value="" size="20" maxlength="20"/></td>
										<td width="140px"><input type="text" name="spcBarCode" id="spcBarCode_${mIndex.count}_${sIndex.count}" value="" size="20" maxlength="20"/></td>
										<td width="90px"><input type="text" name="spcMaxCountPerOrder" id="maxCountPerOrder_${mIndex.count}_${sIndex.count}" value="" size="3" maxlength="3"/></td>
										<td width="90px"><input type="text" name="spcMinCountPerOrder" id="minCountPerOrder_${mIndex.count}_${sIndex.count}" value="" size="3" maxlength="3"/></td>
										<td width="100px"><input type="text" name="spcSafeInventory" id="SafeInventory_${mIndex.count}_${sIndex.count}" value="" size="3" maxlength="3"/></td>
										<td width="50px"><input type="text" name="showSpecSeq" id="showSpecSeq_${mIndex.count}_${sIndex.count}" value="1" size="3" maxlength="3"/></td>
										<td width="50px">
											<a href="###" id="secondDel_${mIndex.count}_${sIndex.count}">删除</a>
										</td>
									</tr>
								</c-rt:forEach>
							</table>
						</td>
					</tr>
				</c-rt:forEach>
			</c-rt:if>
				<tr>
					<td colspan="16">
						<input type="hidden" name="productId" value="${product.productId}"/>
						<input type="submit" id="saveMutilMajor" name="submit2" value="确定"/>
					</td>
				</tr>				
		</tbody>
	</table>
</s:form>